package com.server.questionbank.snframe.controller;

import com.server.questionbank.snframe.controller.Course.pojo.Homework.SetHomework;
import com.server.questionbank.snframe.domain.Homework.HomeworkExercises;
import com.server.questionbank.snframe.domain.Homework.HomeworkPublish;
import com.server.questionbank.snframe.domain.Homework.THomework;
import com.server.questionbank.snframe.domain.ResDTO.ResponseResult;
import com.server.questionbank.snframe.domain.User.TUser;
import com.server.questionbank.snframe.service.HomeworkService.HomeworkService;
import com.server.questionbank.snframe.service.UsersService.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/homework")
public class HomeworkController {
    @Autowired
    UsersService usersService;
    @Autowired
    private HomeworkService homeworkService;
    // 获取作业列表
    @PostMapping("/getHomeworkList")
    public ResponseResult<List<THomework>> getHomeworkList(@RequestBody Map<String, String> map) {
        if (map.get("targetId") == null) return ResponseResult.error("参数错误!");
        Long targetId = null;
        try {
            targetId = Long.parseLong(map.get("targetId"));
        } catch (Exception e) {
            return ResponseResult.error("参数错误!");
        }
        String homeworkType = map.get("homeworkType");
        List<THomework> homeworkList = homeworkService.getHomeworkList(targetId, homeworkType);
        return ResponseResult.success(homeworkList);
    }

    // 获取已发布的作业列表
    @PostMapping("/getPublishHomeworkList")
    public ResponseResult<List<HomeworkPublish>> getPublishHomeworkList(@RequestBody Map<String, String> map) {
        Long targetId = null;
        String homeworkType = map.get("homeworkType");
        // 获取目标id
        if (map.get("targetId") != null )
            try { targetId = Long.parseLong(map.get("targetId")); } catch (Exception ignored) {}
        // 判断是否登录
        TUser user = usersService.getLoginUser();
        if (user == null) return ResponseResult.error("请先登录!");
        // 若是目标ID为传递，则设置为登录用户ID
        if (targetId == null)
        {
            targetId = user.getUserId();
            homeworkType = "user";
        }

        return ResponseResult.success(homeworkService.getPublishHomeworkList(targetId, homeworkType));
    }

    // 获取指定作业的作业发布信息
    @PostMapping("getPublishHomeworkByHomeworkId")
    public ResponseResult<List<HomeworkPublish>> getPublishHomeworkByHomeworkId(@RequestBody Map<String, Long> map) {
        Long homeworkId = map.get("homeworkId");
        if (homeworkId == null) return ResponseResult.error("请选择作业!");
        return ResponseResult.success(homeworkService.getPublishHomeworkByHomeworkId(homeworkId));
    }

    // 获取发布作业
    @PostMapping("/getPublishHomework")
    public ResponseResult<HomeworkPublish> getPublishHomework(@RequestBody Map<String, Long> map) {
        Long publishId = map.get("publishId");
        if (publishId == null) return ResponseResult.error("请选择作业!");
        return ResponseResult.success(homeworkService.getHomeworkPublishById(publishId));
    }

    // 创建作业
    @PostMapping("/setHomework")
    public ResponseResult<Integer> setHomework(@RequestBody SetHomework homework) {
        if (!homeworkService.setHomework(homework.getHomework())) return ResponseResult.error("保存作业失败!");
        List<HomeworkExercises> homeworkExercises = null;
        if (homework.getHomeworkExercises() != null) homeworkExercises = homework.getHomeworkExercises().stream().map(homeworkExercise -> {
            homeworkExercise.setHomeworkId(homework.getHomework().getHomeworkId());
            return homeworkExercise;
        }).collect(Collectors.toList());
        if (homeworkService.updateHomeworkExercises(homeworkExercises)) return ResponseResult.success(1);
        return ResponseResult.error("保存作业失败!");
    }

    // 删除作业
    @PostMapping("/deleteHomework")
    public ResponseResult<Integer> deleteHomework(@RequestBody Map<String, String> map) {
        if (homeworkService.deleteHomework(Long.parseLong(map.get("homeworkId")))) return ResponseResult.success(1);
        return ResponseResult.error("删除失败!");
    }

    // 发布作业
    @PostMapping("/publishHomework")
    public ResponseResult<Integer> publishHomework(@RequestBody HomeworkPublish homeworkPublish) {
        if (homeworkService.publishHomework(homeworkPublish)) return ResponseResult.success(1);
        return ResponseResult.error("发布失败!");
    }

    // 取消发布
    @PostMapping("/cancelPublishHomework")
    public ResponseResult<Integer> cancelPublishHomework(@RequestBody Map<String, Long> map) {
        if (map.get("publishId") == null) return ResponseResult.error("参数错误!");
        if (homeworkService.cancelPublishHomework(map.get("publishId"))) return ResponseResult.success(1);
        return ResponseResult.error("删除失败!");
    }

    // 提交作业
    @PostMapping("/submitHomework")
    public ResponseResult<Integer> submitHomework(@RequestBody Map<String, String> map) {
        return null;
    }
}
